package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationByWorkMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationRecording;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {


        @Resource
        private WorkerCertificationMapper workerCertificationMapper;


        @Resource
        private WorkerCertificationByWorkMapper workerCertificationByWorkMapper;

        @Resource
        private WorkerCertificationMapper worker;



    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 分页查询
     * @param pageQueryReqDTO
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> selectByPage(WorkerCertificationAuditPageQueryReqDTO pageQueryReqDTO) {

        Page<WorkerCertificationRecording> page = PageUtils.parsePageQuery(pageQueryReqDTO, WorkerCertificationRecording.class);

        LambdaQueryWrapper<WorkerCertificationRecording> queryWrapper =
                Wrappers.<WorkerCertificationRecording>lambdaQuery().eq(ObjectUtil.isNotEmpty(pageQueryReqDTO.getAuditStatus()), WorkerCertificationRecording::getAuditStatus, pageQueryReqDTO.getAuditStatus())
                        .eq(ObjectUtil.isNotEmpty(pageQueryReqDTO.getCertificationStatus()), WorkerCertificationRecording::getCertificationStatus, pageQueryReqDTO.getCertificationStatus())
                        .like(ObjectUtil.isNotEmpty(pageQueryReqDTO.getIdCardNo()), WorkerCertificationRecording::getIdCardNo, pageQueryReqDTO.getIdCardNo())
                         .like(ObjectUtil.isNotEmpty(pageQueryReqDTO.getName()),WorkerCertificationRecording::getName,pageQueryReqDTO.getName());


        Page<WorkerCertificationRecording> selectPage = workerCertificationByWorkMapper.selectPage(page, queryWrapper);

        return PageUtils.toPage(selectPage, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 更新状态
     * @param id
     * @param workerCertificationAuditResDTO
     */
    @Transactional
    @Override
    public void updateRecording(Long id, WorkerCertificationAuditResDTO workerCertificationAuditResDTO) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        WorkerCertificationRecording recording = BeanUtil.copyProperties(workerCertificationAuditResDTO, WorkerCertificationRecording.class);
        recording.setAuditorId(currentUserInfo.getId());
        recording.setAuditorName(currentUserInfo.getName());
        recording.setId(id);
        recording.setAuditStatus(1);
        workerCertificationByWorkMapper.updateById(recording);
        WorkerCertificationRecording selectById = workerCertificationByWorkMapper.selectById(id);
        WorkerCertification workerCertification = BeanUtil.copyProperties(selectById, WorkerCertification.class);
        workerCertification.setId(selectById.getServeProviderId());
        workerCertification.setCertificationTime(LocalDateTime.now());


        WorkerCertification workerCertification2 = new WorkerCertification();
        workerCertification2.setId(selectById.getServeProviderId());
        workerCertification2.setCertificationStatus(1);
        worker.updateById(workerCertification2);


        WorkerCertification workerCertification1 = worker.selectById(selectById.getServeProviderId());
        if(ObjectUtil.isNotEmpty(workerCertification1)){
                worker.updateById(workerCertification);
        }else {
            worker.insert(workerCertification);
        }

    }
}
